﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using StoveTop.Models;
using System.Xml;
using Android.Content.Res;

namespace StoveTop
{
	[Activity (Label = "SeedDatabase")]			
	public class SeedDatabase : Activity
	{
		public static readonly string[] RECIPE_FILES = { "Recipe1.xml", "Recipe2.xml", "SpinachQuiche.xml"};
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);


			XmlReaderSettings settings = new XmlReaderSettings ();
			settings.IgnoreWhitespace = true;
			foreach (string fileName in RECIPE_FILES) {
				StreamReader strm = new StreamReader (Assets.Open (fileName));
				XmlReader reader = XmlReader.Create (strm, settings);
				parseXmlRecipe (reader);
			}

			// Fill database with vocab
			StoveTopDBHelper db = new StoveTopDBHelper (this);
			StoveTop.Models.Vocab voc_add = new StoveTop.Models.Vocab ();
			voc_add.SetName ("Add");
			voc_add.SetDescription ("To put ingredients together; to put one ingredient with the others");
			db.AddVocab (voc_add);

			StoveTop.Models.Vocab voc_bake = new StoveTop.Models.Vocab ();
			voc_bake.SetName ("Bake");
			voc_bake.SetDescription ("To cook in an oven by using heat");
			db.AddVocab (voc_bake);

			StoveTop.Models.Vocab voc_barbecue = new StoveTop.Models.Vocab ();
			voc_barbecue.SetName ("Barbecue");
			voc_barbecue.SetDescription ("To cook food (primary meat) on a grill by using fire or hot coals");
			db.AddVocab (voc_barbecue);

			StoveTop.Models.Vocab voc_beat = new StoveTop.Models.Vocab ();
			voc_beat.SetName ("Beat");
			voc_beat.SetDescription ("To mix quickly and continually, commonly used with eggs");
			db.AddVocab (voc_beat);

			StoveTop.Models.Vocab voc_boil = new StoveTop.Models.Vocab ();
			voc_boil.SetName ("Boil");
			voc_boil.SetDescription ("To heat water until little bubbles form");
			db.AddVocab (voc_boil);

			StoveTop.Models.Vocab voc_break = new StoveTop.Models.Vocab ();
			voc_break.SetName ("Break");
			voc_break.SetDescription ("To separate into smaller parts by force");
			db.AddVocab (voc_break);

			StoveTop.Models.Vocab voc_broil = new StoveTop.Models.Vocab ();
			voc_broil.SetName ("Broil");
			voc_broil.SetDescription ("To cook meat or vegetables on a rack with an extremely high temperature");
			db.AddVocab (voc_broil);

			StoveTop.Models.Vocab voc_carve = new StoveTop.Models.Vocab ();
			voc_carve.SetName ("Carve");
			voc_carve.SetDescription ("To cut meat into slices");
			db.AddVocab (voc_carve);

			StoveTop.Models.Vocab voc_chop = new StoveTop.Models.Vocab ();
			voc_chop.SetName ("Chop");
			voc_chop.SetDescription ("To cut into small pieces, generally used with vegetables");
			db.AddVocab (voc_chop);

			StoveTop.Models.Vocab voc_combine = new StoveTop.Models.Vocab ();
			voc_combine.SetName ("Combine");
			voc_combine.SetDescription ("To put two or more things together");
			db.AddVocab (voc_combine);

			StoveTop.Models.Vocab voc_cook = new StoveTop.Models.Vocab ();
			voc_cook.SetName ("Cook");
			voc_cook.SetDescription ("To prepare food by heating it, so the food is not raw");
			db.AddVocab (voc_cook);

			StoveTop.Models.Vocab voc_crush = new StoveTop.Models.Vocab ();
			voc_crush.SetName ("Crush");
			voc_crush.SetDescription ("To cause to separate or flatten by extreme force, often used with garlic");
			db.AddVocab (voc_crush);

			StoveTop.Models.Vocab voc_cut = new StoveTop.Models.Vocab ();
			voc_cut.SetName ("Cut");
			voc_cut.SetDescription ("To separate or divide using a knife");
			db.AddVocab (voc_cut);

			StoveTop.Models.Vocab voc_fry = new StoveTop.Models.Vocab ();
			voc_fry.SetName ("Fry");
			voc_fry.SetDescription ("To cook by putting the food into extremely hot oil");
			db.AddVocab (voc_fry);

			StoveTop.Models.Vocab voc_grate = new StoveTop.Models.Vocab ();
			voc_grate.SetName ("Grate");
			voc_grate.SetDescription ("To divide into small parts by rubbing on a serrated surface, usually used with cheese");
			db.AddVocab (voc_grate);

			StoveTop.Models.Vocab voc_grease = new StoveTop.Models.Vocab ();
			voc_grease.SetName ("Grease");
			voc_grease.SetDescription ("To coat with oil or butter");
			db.AddVocab (voc_grease);

			StoveTop.Models.Vocab voc_grill = new StoveTop.Models.Vocab ();
			voc_grill.SetName ("Grill");
			voc_grill.SetDescription ("To cook by putting the food on a grill; similar to barbecue");
			db.AddVocab (voc_grill);

			StoveTop.Models.Vocab voc_knead = new StoveTop.Models.Vocab ();
			voc_knead.SetName ("Knead");
			voc_knead.SetDescription ("To press and stretch dough, usually used with making bread");
			db.AddVocab (voc_knead);

			StoveTop.Models.Vocab voc_mix = new StoveTop.Models.Vocab ();
			voc_mix.SetName ("Mix");
			voc_mix.SetDescription ("To combine two or more things using a spoon, spatula, or electric mixer");
			db.AddVocab (voc_mix);

			StoveTop.Models.Vocab voc_measure = new StoveTop.Models.Vocab ();
			voc_measure.SetName ("Measure");
			voc_measure.SetDescription ("To obtain an exact quantity");
			db.AddVocab (voc_measure);

			StoveTop.Models.Vocab voc_melt = new StoveTop.Models.Vocab ();
			voc_melt.SetName ("Melt");
			voc_melt.SetDescription ("To make something liquid through heating");
			db.AddVocab (voc_melt);

			StoveTop.Models.Vocab voc_microwave = new StoveTop.Models.Vocab ();
			voc_microwave.SetName ("Microwave");
			voc_microwave.SetDescription ("To heat up food with a microwave oven");
			db.AddVocab (voc_microwave);

			StoveTop.Models.Vocab voc_mince = new StoveTop.Models.Vocab ();
			voc_mince.SetName ("Mince");
			voc_mince.SetDescription ("To grind food, normally meat, into small pieces. A machine is often used to do this");
			db.AddVocab (voc_mince);

			StoveTop.Models.Vocab voc_open = new StoveTop.Models.Vocab ();
			voc_open.SetName ("Open");
			voc_open.SetDescription ("To remove the top from a can or jar");
			db.AddVocab (voc_open);

			StoveTop.Models.Vocab voc_peel = new StoveTop.Models.Vocab ();
			voc_peel.SetName ("Peel");
			voc_peel.SetDescription ("To take the skin off fruits or vegetables");
			db.AddVocab (voc_peel);

			StoveTop.Models.Vocab voc_pour = new StoveTop.Models.Vocab ();
			voc_pour.SetName ("Pour");
			voc_pour.SetDescription ("To transfer liquid from one container or another");
			db.AddVocab (voc_pour);

			StoveTop.Models.Vocab voc_put = new StoveTop.Models.Vocab ();
			voc_put.SetName ("Put");
			voc_put.SetDescription ("To place something in a particular position or location");
			db.AddVocab (voc_put);

			StoveTop.Models.Vocab voc_roast = new StoveTop.Models.Vocab ();
			voc_roast.SetName ("Roast");
			voc_roast.SetDescription ("To cook in the oven or over a fire");
			db.AddVocab (voc_roast);

			StoveTop.Models.Vocab voc_saute = new StoveTop.Models.Vocab ();
			voc_saute.SetName ("Saute");
			voc_saute.SetDescription ("To quickly fry food by placing it in hot oil in a frying pan");
			db.AddVocab (voc_saute);

			StoveTop.Models.Vocab voc_scramble = new StoveTop.Models.Vocab ();
			voc_scramble.SetName ("Scramble");
			voc_scramble.SetDescription ("To mix the white and yellow parts of eggs together while cooking them in a pan");
			db.AddVocab (voc_scramble);

			StoveTop.Models.Vocab voc_slice = new StoveTop.Models.Vocab ();
			voc_slice.SetName ("Slice");
			voc_slice.SetDescription ("To cut into thin, wide portions");
			db.AddVocab (voc_slice);

			StoveTop.Models.Vocab voc_steam = new StoveTop.Models.Vocab ();
			voc_steam.SetName ("Steam");
			voc_steam.SetDescription ("To cook by placing the food above boiling water. Steam is the vapor that comes from hot water");
			db.AddVocab (voc_steam);

			StoveTop.Models.Vocab voc_stir = new StoveTop.Models.Vocab ();
			voc_stir.SetName ("Stir");
			voc_stir.SetDescription ("To mix liquid ingredients by moving a spoon around in a circular motion");
			db.AddVocab (voc_stir);

			StoveTop.Models.Vocab voc_stir_fry = new StoveTop.Models.Vocab ();
			voc_stir_fry.SetName ("Stir Fry");
			voc_stir_fry.SetDescription ("To cook small pieces of food by moving it quickly in hot oil");
			db.AddVocab (voc_stir_fry);

			StoveTop.Models.Vocab voc_wash = new StoveTop.Models.Vocab ();
			voc_wash.SetName ("Wash");
			voc_wash.SetDescription ("To immerse food in water and make sure it becomes clean");
			db.AddVocab (voc_wash);

			StoveTop.Models.Vocab voc_weigh = new StoveTop.Models.Vocab ();
			voc_weigh.SetName ("Weigh");
			voc_weigh.SetDescription ("To measure the weight(grams or pounds) of something");
			db.AddVocab (voc_weigh);

			this.Finish ();

			/*
			List <Ingredient> ingreds = db.GetAllIngredients ();
			if (ingreds.Count == 0) {

				// Add ingredients
				Ingredient pb = new Ingredient ();
				pb.SetName ("Peanut Butter");
				pb.SetQuantityInList (0);
				pb.SetQuantityOnHand (0);
				pb.SetUnits ("tbsps");
				Ingredient jelly = new Ingredient ();
				jelly.SetName ("Jelly");
				jelly.SetQuantityInList (0);
				jelly.SetQuantityOnHand (0);
				jelly.SetUnits ("tbsps");
				Ingredient bread = new Ingredient ();
				bread.SetName ("Bread");
				bread.SetQuantityInList (0);
				bread.SetQuantityOnHand (0);
				bread.SetUnits ("slices");
				Ingredient fluff = new Ingredient ();
				fluff.SetName ("Fluff");
				fluff.SetQuantityInList (0);
				fluff.SetQuantityOnHand (0);
				fluff.SetUnits ("tbsps");

				int id = db.AddIngredient (pb);
				Console.WriteLine ("pb id=" + id.ToString());

				id = db.AddIngredient (jelly);
				Console.WriteLine ("jelly id=" + id.ToString());

				id = db.AddIngredient (bread);
				Console.WriteLine ("bread id=" + id.ToString());

				id = db.AddIngredient (fluff);

				// Add Recipe
				StoveTop.Models.Recipe pb_j = new StoveTop.Models.Recipe ();
				pb_j.SetName ("PB & J");
				pb_j.SetCookTime (0);
				pb_j.SetPrepTime (180);
				pb_j.SetRating (5.0m);
				pb_j.SetDifficulty (1.0m);
				int recipeId = db.AddRecipe (pb_j);

				// Add RecipeIngreds
				db.AddRecipeIngredient (recipeId, "Peanut Butter", 2, "tbsp", "");
				db.AddRecipeIngredient (recipeId, "Jelly", 2, "tbsps", "");
				db.AddRecipeIngredient (recipeId, "Bread", 2, "slices", "");

				StoveTop.Models.Recipe pb_f = new StoveTop.Models.Recipe ();
				pb_f.SetName ("PB & Fluff");
				pb_f.SetCookTime (0);
				pb_f.SetPrepTime (180);
				pb_f.SetRating (3.0m);
				pb_f.SetDifficulty (1.0m);
				recipeId = db.AddRecipe(pb_f);

				// Add RecipeIngreds
				db.AddRecipeIngredient (recipeId, "Peanut Butter", 2, "tbsp", "");
				db.AddRecipeIngredient (recipeId, "Fluff", 2, "tbsps", "Extra fluffy");
				db.AddRecipeIngredient (recipeId, "Bread", 2, "slices", "");

				// Add Steps
				Step step1 = new Step ();
				step1.SetInstructions ("Spread peanut butter on one slice of bread");
				step1.SetStepNum (1);
				step1.SetTimer (0);
				Step step2 = new Step ();
				step2.SetInstructions ("Spread jelly on other slice of bread");
				step2.SetStepNum (2);
				step2.SetTimer (0);
				Step step3 = new Step ();
				step3.SetInstructions ("Combine slices of bread into a sandwich");
				step3.SetStepNum (3);
				step3.SetTimer (0);
				Step step4 = new Step ();
				step4.SetInstructions ("Spread fluff on other slice of bread");
				step4.SetStepNum (2);
				step4.SetTimer (0);
				db.AddStep (step1, 1);
				db.AddStep (step2, 1);
				db.AddStep (step3, 1);
				db.AddStep (step1, 2);
				db.AddStep (step4, 2);
				db.AddStep (step3, 2);

				this.Finish ();

				// Add RecipeSteps
*/
		}
		private void parseXmlRecipe(XmlReader reader) {
			StoveTopDBHelper db = new StoveTopDBHelper (this);
			StoveTop.Models.Recipe recipe = new StoveTop.Models.Recipe ();
			// Move to root element
			reader.MoveToContent ();
			// Get recipe info
			reader.ReadToFollowing ("name");
			recipe.SetName (reader.ReadElementContentAsString());
			recipe.SetPrepTime (reader.ReadElementContentAsInt());
			recipe.SetCookTime (reader.ReadElementContentAsInt());
			recipe.SetRating (reader.ReadElementContentAsDecimal());
			recipe.SetDifficulty (reader.ReadElementContentAsDecimal());
			int recipeId = db.AddRecipe (recipe);
			Console.WriteLine("Added recipe: " + recipe.ToString());

			// Get ingredients info
			reader.ReadToDescendant ("ingredient");
			do {
				reader.ReadToDescendant("name");
				String name = reader.ReadElementContentAsString();
				decimal quantity = reader.ReadElementContentAsDecimal();
				String units = reader.ReadElementContentAsString();
				String prepNotes = reader.ReadElementContentAsString();
				db.AddIngredient(name);
				db.AddRecipeIngredient(recipeId, name, quantity, units, prepNotes);
				Console.WriteLine ("Read ingredient: " + name + " " + quantity.ToString () + " " + units + " " + prepNotes);

			} while (reader.ReadToNextSibling ("ingredient"));

			// Get tags
			reader.ReadToFollowing ("tags");
			reader.ReadToDescendant ("tag");
			do {
				reader.ReadStartElement();
				String tagname = reader.Value;
				StoveTop.Models.Tag tag = new StoveTop.Models.Tag();
				tag.SetName(tagname);
				int tagId = db.AddTag(tag);
				db.AddRecipeTag(recipeId, tagId);
				Console.WriteLine("Read tag: " + tagname);
			} while (reader.ReadToNextSibling("tag"));

			// Get steps
			int stepNum = 1;
			reader.ReadToFollowing ("steps");
			reader.ReadToDescendant ("step");
			do {
				reader.ReadToDescendant("instruction");
				string instructions = reader.ReadElementContentAsString();
				int timer = reader.ReadElementContentAsInt();
				StoveTop.Models.Step step = new StoveTop.Models.Step();
				step.SetInstructions(instructions);
				step.SetTimer(timer);
				step.SetStepNum(stepNum);
				stepNum++;
				db.AddStep(step, recipeId);
				Console.WriteLine("Read step: " + instructions + " " + timer.ToString());
			} while (reader.ReadToNextSibling("step"));
		}
	}
}



